class Node:
    def __init__(self, value):
        self.value = value
        self.next = None


class CreateLinkList:
    def __init__(self, nums):
        self._nums = nums

    # 用尾插法链表创建
    def rear(self):
        head = Node(self._nums[0])
        tail = head
        for value in self._nums[1:]:
            node = Node(value)
            tail.next = node
            tail = node
        return head

    # 用头插法创建链表
    def head(self):
        head_ = Node(self._nums[0])
        for value in self._nums[1:]:
            node = Node(value)
            node.next = head_
            head_ = node
        return head_


def reverse_iterative(head):
    """ 迭代法翻转链表 """
    if not head or not head:
        return head

    dummy = Node(0)
    dummy.next = head
    while head and head.next:
        dummy_next = dummy.next
        head_next = head.next
        dummy.next = head_next
        head.next = head_next.next
        head_next.next = dummy_next
    return dummy.next


def reverse_recursive(head):
    """ 递归法翻转链表 """
    if head.next is None or head is None:
        return head

    new_head = reverse_recursive(head.next)
    head.next.next = head
    head.next = None
    return new_head


def print_linklist(head):
    """ 查看链表元素 """
    while head:
        print(head.value, end=' -> ')
        head = head.next
